package com.george.util;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.*;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.DeleteIndex;
import io.searchbox.indices.mapping.GetMapping;
import io.searchbox.indices.mapping.PutMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  ElasticSearch JestClient客户端 工具类
 * </p>
 *
 * @author GeorgeChan 2019/6/12 10:43
 * @version 1.0
 * @since jdk1.8
 */
public class JestClientUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(JestClientUtil.class);
    /**
     * 创建索引
     * @param jestClient 客户端
     * @param indexName 索引名称
     * @return 是否操作成功
     */
    public static boolean createIndex(JestClient jestClient, String indexName) {
        JestResult jestResult;
        try {
            jestResult = jestClient.execute(new CreateIndex.Builder(indexName).build());
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return jestResult.isSucceeded();
    }

    /**
     * mapping 映射
     * @param jestClient 客户端
     * @param indexName 索引名称
     * @param typeName 类型名称
     * @param source mapping source
     * @return
     */
    public static boolean createIndexMapping(JestClient jestClient, String indexName, String typeName, String source) {
        PutMapping putMapping = new PutMapping.Builder(indexName, typeName, source).build();
        JestResult jestResult;
        try {
            jestResult = jestClient.execute(putMapping);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return jestResult.isSucceeded();
    }

    /**
     * 查询数据
     * @param jestClient 客户端
     * @param indexName 索引名称
     * @param typeName 类型名称
     * @return 返回的json字符串
     */
    public static String getIndexMapping(JestClient jestClient, String indexName, String typeName) {
        GetMapping getMapping = new GetMapping.Builder().addIndex(indexName).addType(typeName).build();
        JestResult jestResult = null;
        try {
            jestResult = jestClient.execute(getMapping);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jestResult.getJsonString();
    }

    /**
     * 批量新增数据
     * @param jestClient 客户端
     * @param indexName 索引名称
     * @param typeName 类型名称
     * @param objs 数据集合
     * @return 是否新增成功
     */
    public static boolean insertBatch(JestClient jestClient, String indexName, String typeName, List<Object> objs) {
        Bulk.Builder bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(typeName);
        for (Object obj : objs) {
            Index index = new Index.Builder(obj).build();
            bulk.addAction(index);
        }
        BulkResult br;
        try {
            br = jestClient.execute(bulk.build());
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return br.isSucceeded();
    }

    /**
     * 全文搜索
     * @param jestClient 客户端
     * @param indexName 索引名称
     * @param typeName 类型名称
     * @param query 查询条件
     * @return 查询结果json字符串
     */
    public static String search(JestClient jestClient, String indexName, String typeName, String query) {
        Search search = new Search.Builder(query).addIndex(indexName).addType(typeName).build();
        JestResult jestResult = null;
        try {
            jestResult = jestClient.execute(search);
        } catch (IOException e) {
            e.printStackTrace();
        }
        LOGGER.info("jestResult.getJsonString() ===> {}", jestResult.getJsonString());
        LOGGER.info("jestResult.getSourceAsString() ===> {}", jestResult.getSourceAsString());
//        return jestResult.getSourceAsString();
        return jestResult.getJsonString();
    }

    /**
     * 删除索引
     * @param jestClient 客户端
     * @param indexName 索引名称
     * @return 是否操作成功
     */
    public static boolean deleteIndex(JestClient jestClient, String indexName) {
        JestResult jestResult;
        try {
            jestResult = jestClient.execute(new DeleteIndex.Builder(indexName).build());
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return jestResult.isSucceeded();
    }

    /**
     * 根据id删除数据
     * @param jestClient 客户端
     * @param indexName 索引名称
     * @param typeName 类型名称
     * @param id 数据id
     * @return 是否操作成功
     */
    public static boolean deleteData(JestClient jestClient, String indexName, String typeName, String id) {
        DocumentResult dr;
        try {
            dr = jestClient.execute(new Delete.Builder(id).index(indexName).type(typeName).build());
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return dr.isSucceeded();
    }

    /**
     * 单个插入
     * @param jestClient 客户端
     * @param indexName 索引名称
     * @param typeName 类型名称
     * @param object 保存对象
     * @return 是否操作成功
     */
    public static boolean insertData(JestClient jestClient, String indexName, String typeName, Object object) {
        Bulk.Builder bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(typeName);
        Index index = new Index.Builder(object).build();
        bulk.addAction(index);
        BulkResult br;
        try {
            br = jestClient.execute(bulk.build());
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return br.isSucceeded();
    }

}
